package ca.unbc.cpsc672.fuzzy.rule;

import ca.unbc.cpsc672.fuzzy.function.MembershipFunction;

public class IfThenRule {

	private MembershipFunction opt;
	private double supportLevel;
	private IfClause ifc;

	public IfThenRule(IfClause ifc, MembershipFunction opt) {
		this.ifc = ifc;
		this.opt = opt;
	}

	public void setInput(double... iptx) {
		// Apply Fuzzy Operator
		supportLevel = ifc.getSupportLevel(iptx);
	}

	public double calcImplicationFunction(double y) {
		// original membership function
		double mu = opt.calcProperbility(y);
		// Apply Implication Method, cut
		return Math.min(supportLevel, mu);
	}

	public double getRange() {
		return opt.getFuzzySet().getRange();
	}

	public String toCSVString() {
		StringBuffer sb = new StringBuffer();
		sb.append(ifc.toCSVString());
		sb.append(opt.getFullName());
		return sb.toString();
	}

	@Override
	public String toString() {
		return toCSVString();
	}

	public IfClause getIfClause() {
		return ifc;
	}

	public MembershipFunction getOutputMembershipFunction() {
		return opt;
	}
	
	public void update(MembershipFunction opt){
		this.opt = opt;
	}
	
	public void update(IfClause ifc){
		this.ifc = ifc;
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
}